using LDNS.Utilities;
using LDNS.SDK.Commands.Models;

namespace LDNS.Commands
{
    /// <summary>
    /// 添加记录命令处理器
    /// 处理ADD命令，添加新的DNS解析记录
    /// 作者：huobing
    /// </summary>
    public class AddCommandHandler : CommandHandlerBase<AddCommand, DefaultCommandResult>
    {
        /// <summary>
        /// 命令名称
        /// </summary>
        public override string CommandName => AddCommand.NAME;
        
        /// <summary>
        /// 构造函数
        /// </summary>
        public AddCommandHandler() { }
        
        /// <summary>
        /// 执行命令
        /// </summary>
        /// <param name="args">命令参数，格式：IP 域名 [record_type] [TTL]</param>
        /// <returns>执行结果</returns>
        public override BaseCommandResult Execute(string[] args)
        {
            try
            {
                string ip = args[0];
                string domain = args[1];
                string recordType = args.Length >= 3 ? args[2] : "A";
                int ttl = args.Length >= 4 ? int.Parse(args[3]) : 3600;
                string description = args.Length >= 5 ? args[4] : string.Empty;
                
                if (DnsRecordManager.Instance.AddDnsRecord(domain, ip, recordType, ttl, description))
                {
                    // 保存配置到文件，实现持久化
                    DnsRecordManager.Instance.SaveConfig();
                    return CreateSuccessResult($"OK: 成功添加记录 {domain} -> {ip} (类型: {recordType}, TTL: {ttl}, 描述: {description})");
                }
                else
                {
                    return CreateErrorResult("ERROR: 添加记录失败");
                }
            }
            catch (System.Exception ex)
            {
                return CreateErrorResult($"ERROR: 添加记录失败: {ex.Message}");
            }
        }
    }
}